var jqGridFlag = true;
$(function() {
	$("#jqGrid").jqGrid(
			{
				url : baseURL + 'compaymenthistory/list',
				datatype : "json",
				colModel : [ {
					label : '序号',
					name : 'serialNo',
					width : 50,
					align : 'center',
					key : true,
					sortable : false,
					frozen : true,
					hidden : true
				}, {
					label : '单据号',
					name : 'billNo',
					width : 100,
					align : 'center',
					frozen : true,
					sortable : false
				}, {
					label : '合同号',
					name : 'contractNo',
					width : 100,
					align : 'center',
					frozen : true,
					sortable : false
				}, {
					label : '楼号',
					name : 'buildIds',
					width : 100,
					align : 'center',
					frozen : true,
					sortable : false
				}, {
					label : '房号',
					name : 'assetIds',
					width : 100,
					align : 'center',
					frozen : true,
					sortable : false
				}, {
					label : '业主姓名',
					name : 'proprietor',
					width : 80,
					align : 'center',
					sortable : false
				}, {
					label : '付款时间',
					name : 'due',
					width : 90,
					align : 'center',
					sortable : false
				}, {
					label : '付款方式',
					name : 'paymentMethod',
					width : 80,
					align : 'center',
					sortable : false
				}, {
					label : '款项',
					name : 'fund',
					width : 80,
					align : 'center',
					sortable : false
				}, {
					label : '金额',
					name : 'payAmount',
					width : 80,
					align : 'right',
					formatter : 'number',
					sortable : false
				}, {
					label : '计费周期',
					name : 'billingCycle',
					width : 180,
					align : 'center',
					sortable : false
				}, {
					label : '备注',
					name : 'remark',
					width : 200,
					align : 'center',
					sortable : false
				} ],
				viewrecords : true,
				height : 385,
				rowNum : 10,
				rowList : [ 10, 30, 50 ],
				rownumbers : true,
				rownumWidth : 25,
				autowidth : true,
				multiselect : true,
				shrinkToFit : false,
				sortable : false,
				footerrow : true,
				pager : "#jqGridPager",
				jsonReader : {
					root : "page.list",
					page : "page.currPage",
					total : "page.totalPage",
					records : "page.totalCount"
				},
				prmNames : {
					page : "page",
					rows : "limit",
					order : "order"
				},
				gridComplete : function() {
					// jqGrid底部合计
					var rowNum = parseInt($(this).getGridParam("records"), 10);
					if (rowNum > 0) {
						$(".ui-jqgrid-sdiv").show();
						var payAmountCount = $(this).getCol("payAmount", false,
								"sum").toFixed(2);
						$(this).footerData("set", {
							"payAmount" : payAmountCount
						}); // 将合计值显示出来
					} else {
						$(".ui-jqgrid-sdiv").hide();
					}
					if (jqGridFlag) {
						// 冻结列
						$("#jqGrid").jqGrid('setFrozenColumns');
						airbus();
						frozenDislocation();
						jqGridFlag = false;
					}
					setTimeout(function() {
						$('.frozen-bdiv.ui-jqgrid-bdiv').height(
								$('.frozen-bdiv.ui-jqgrid-bdiv').height() - 1)
					}, 100);
				}
			});
});
function airbus() {
	$(".ui-jqgrid-sdiv").find("td[aria-describedby='jqGrid_billNo']").empty()
			.text("合计");
	$(".frozen-sdiv.ui-jqgrid-sdiv")
			.find(
					"table[class='ui-jqgrid-ftable ui-common-table table table-bordered']")
			.eq(1).remove();
	$(".frozen-sdiv.ui-jqgrid-sdiv").find(
			"td[aria-describedby='jqGrid_billNo']").empty().text("合计");
};
function frozenDislocation() {
	// 冻结错位解决
	var bdivTop = -1;
	var sdivTop = -1;
	// opera 不需要hack
	if (navigator.userAgent.indexOf("Opera") >= 0) {
		divTop = 0;
		bdivTop = 0;
	}
	$('.frozen-bdiv.ui-jqgrid-bdiv').css({
		top : $('.frozen-bdiv.ui-jqgrid-bdiv').position().top + bdivTop
	});
	$('.frozen-sdiv.ui-jqgrid-sdiv').css({
		top : $('.frozen-sdiv.ui-jqgrid-sdiv').position().top + sdivTop
	});
};
var vm = new Vue(
		{
			el : '#rrapp',
			data : {
				q : {},
				showList : true,
				title : null,
				comPaymentHistory : {
					due : null,
					chargingSd : null,
					chargingEd : null,
					paymentMethod : null
				},
			},
			methods : {
				exportExcel : function() {
					vm.q.dueSd = $('#dueSd').val();
					vm.q.dueEd = $('#dueEd').val();
					var url = "compaymenthistory/export?token=" + token;
					if (vm.q.dueSd) {
						url += "&dueSd=" + vm.q.dueSd;
					}
					if (vm.q.dueEd) {
						url += "&dueEd=" + vm.q.dueEd;
					}
					if (vm.q.buildIds) {
						url += "&buildIds=" + vm.q.buildIds;
					}
					if (vm.q.assetIds) {
						url += "&assetIds=" + vm.q.assetIds;
					}
					if (vm.q.contractNo) {
						url += "&contractNo=" + vm.q.contractNo;
					}
					if (vm.q.proprietor) {
						url += "&proprietor=" + vm.q.proprietor;
					}
					if (vm.q.billNo) {
						url += "&billNo=" + vm.q.billNo;
					}
					location.href = baseURL + url;
				},
				query : function() {
					vm.reload();
				},
				add : function() {
					vm.showList = false;
					vm.title = "新增";
					vm.comPaymentHistory = {
						paymentMethod : null
					};
					$("#table").find("textarea").val('');
					$("#table").find("label").text('');
				},
				update : function(event) {
					var serialNo = getSelectedRow();
					if (serialNo == null) {
						return;
					}
					vm.showList = false;
					vm.title = "修改";
					vm.getInfo(serialNo)
				},
				saveOrUpdate : function(event) {
					var url = vm.comPaymentHistory.serialNo == null ? "compaymenthistory/save"
							: "compaymenthistory/update";
					$.ajax({
						type : "POST",
						url : baseURL + url,
						data : JSON.stringify(vm.comPaymentHistory),
						success : function(r) {
							if (r.code === 0) {
								alert('操作成功', function(index) {
									vm.reload();
								});
							} else {
								alert(r.msg);
							}
						}
					});
				},
				del : function(event) {
					var serialNos = getSelectedRows();
					if (serialNos == null) {
						return;
					}
					confirm('确定要删除选中的记录？', function() {
						$.ajax({
							type : "POST",
							url : baseURL + "compaymenthistory/delete",
							data : JSON.stringify(serialNos),
							success : function(r) {
								if (r.code == 0) {
									alert('操作成功', function(index) {
										$("#jqGrid").trigger("reloadGrid");
									});
								} else {
									alert(r.msg);
								}
							}
						});
					});
				},
				getInfo : function(serialNo) {
					$.get(baseURL + "compaymenthistory/info/" + serialNo,
							function(r) {
								vm.comPaymentHistory = r.comPaymentHistory;
								$("#month").text(r.comPaymentHistory.month);
								$("#amountPayable").text(
										r.comPaymentHistory.amountPayable);
								$("#amountPayableTotal").text(
										r.comPaymentHistory.amountPayable);
								$("#payAmountTotal").text(
										r.comPaymentHistory.payAmount);
							});
				},
				reload : function(event) {
					var dueSd = $("#dueSd").val();
					var dueEd = $("#dueEd").val();
					vm.showList = true;
					var page = $("#jqGrid").jqGrid('getGridParam', 'page');
					$("#jqGrid").jqGrid('setGridParam', {
						postData : {
							'dueSd' : dueSd,
							'dueEd' : dueEd,
							'assetIds' : vm.q.assetIds,
							'contractNo' : vm.q.contractNo,
							'proprietor' : vm.q.proprietor,
							'billNo' : vm.q.billNo,
							'buildIds' : vm.q.buildIds
						},
						page : page
					}).trigger("reloadGrid");
				},
			}
		});
$('#due').datepicker().on('changeDate.datepicker.amui', function(event) {
	var date = new Date(event.date);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if (month < 10) {
		month = '0' + month;
	}
	var day = date.getDate();
	if (day < 10) {
		day = '0' + day;
	}
	$('#due').val(year + '-' + month + '-' + day);
	vm.comPaymentHistory.due = year + '-' + month + '-' + day;
});
$('#chargingSd').datepicker().on('changeDate.datepicker.amui', function(event) {
	var date = new Date(event.date);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if (month < 10) {
		month = '0' + month;
	}
	var day = date.getDate();
	if (day < 10) {
		day = '0' + day;
	}
	$('#chargingSd').val(year + '-' + month + '-' + day);
	vm.comPaymentHistory.chargingSd = year + '-' + month + '-' + day;
	getMonth();
});
$('#chargingEd').datepicker().on('changeDate.datepicker.amui', function(event) {
	var date = new Date(event.date);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if (month < 10) {
		month = '0' + month;
	}
	var day = date.getDate();
	if (day < 10) {
		day = '0' + day;
	}
	$('#chargingEd').val(year + '-' + month + '-' + day);
	vm.comPaymentHistory.chargingEd = year + '-' + month + '-' + day;
	getMonth();
});
$('#chargingSd').keyup(function(e) {
	if (e.keyCode == 8) {
		$('#chargingSd').val("");
		vm.comPaymentHistory.chargingSd = "";
		getMonth();
		getAmountPayable();
	}
});
$('#chargingEd').keyup(function(e) {
	if (e.keyCode == 8) {
		$('#chargingEd').val("");
		vm.comPaymentHistory.chargingEd = "";
		getMonth();
		getAmountPayable();
	}
});
$("#univalent").keyup(function() {
	getAmountPayable();
});
$("#acreage").keyup(function() {
	getAmountPayable();
});
$("#payAmount").keyup(function() {
	$("#payAmountTotal").text($("#payAmount").val());
});
function getMonth() {
	if (!$("#chargingSd").val().match(new RegExp('^[0-9]+-[0-9]+-[0-9]+.*$'))) {
		$("#month").text("");
		return;
	}
	if (!$("#chargingEd").val().match(new RegExp('^[0-9]+-[0-9]+-[0-9]+.*$'))) {
		$("#month").text("");
		return;
	}
	var startTmp = $("#chargingSd").val().split("-");
	var endTmp = $("#chargingEd").val().split("-");
	var dS = new Date(startTmp[0], startTmp[1] - 1, startTmp[2]);
	var dE = new Date(endTmp[0], endTmp[1] - 1, endTmp[2]);
	if (dE.getTime() - dS.getTime() < 0) {
		alert("计费开始时间不能大于计费结束时间！");
	}
	var distance = (dE.getTime() - dS.getTime()) / (1000 * 60 * 60 * 24 * 30);
	var month = Math.round(distance);
	if (month < 1) {
		month = 1;
	}
	$("#month").text(month);
	getAmountPayable();
}
function getAmountPayable() {
	var univalent = $("#univalent").val();
	if (!univalent.match(new RegExp('^[+-]?\\d+(\\.\\d+)?$'))) {
		$("#amountPayable").text("");
		$("#amountPayableTotal").text("");
		return;
	}
	var acreage = $("#acreage").val();
	if (!acreage.match(new RegExp('^[+-]?\\d+(\\.\\d+)?$'))) {
		$("#amountPayable").text("");
		$("#amountPayableTotal").text("");
		return;
	}
	var month = $("#month").text();
	if (!month.match(new RegExp('^[+-]?\\d+(\\.\\d+)?$'))) {
		$("#amountPayable").text("");
		$("#amountPayableTotal").text("");
		return;
	}
	var amountPayable = parseFloat(univalent) * parseFloat(acreage)
			* parseFloat(month);
	$("#amountPayable").text(amountPayable);
	$("#amountPayableTotal").text(amountPayable);
}